<?php

	require '../utils/conn.php';
	include_once '../models/topic.php';
	$type = $_REQUEST['type'];
	$content = $_REQUEST['content'];
	$title = $_REQUEST['title'];
	$setId = $_REQUEST['setid'];
	
	$topic = new Topic();
	$topic->setType($type);
	$topic->setContent($content);
	$topic->setTimestamp(time());
	$topic->setTitle($title);
	$query = "INSERT INTO t_topic(t_content, t_title, t_type, t_timestamp)" . " VALUES('$topic->content','$topic->title','$topic->type','$topic->timestamp')";

	$result = mysql_query($query) or die("insert topic failed：". mysql_error());
	$topic->topicId = mysql_insert_id();
	
	if ($type == 1) {
		$query = "UPDATE t_set SET listening_id=".$topic->topicId." WHERE s_id=".$setId;
	}
	else if ($type == 2) {
		$query = "UPDATE t_set SET speaking_id=".$topic->topicId." WHERE s_id=".$setId;
	}
	else if ($type == 3) {
		$query = "UPDATE t_set SET reading_id=".$topic->topicId." WHERE s_id=".$setId;
	}
	else if ($type == 4) {
		$query = "UPDATE t_set SET writing_id=".$topic->topicId." WHERE s_id=".$setId;
	}
	mysql_query($query) or die("insert topic failed：". mysql_error());
	
?>